﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using DTcms.Common;

namespace DTcms.Web.admin.xinxi
{
    public partial class xinxi_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            this.id = DTRequest.GetQueryInt("id");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.xinxi().Exists(this.id))
                {
                    JscriptMsg("内容不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                GetCategoryID();
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
                else
                {
                    txtAddTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                }
            }
        }

        #region 绑定类别=================================
        private void GetCategoryID()
        {
            BLL.gongying_category bll = new BLL.gongying_category();
            DataTable dt = bll.GetList(0, "", "sort_id asc,id desc").Tables[0];

            this.ddlCategoryId.Items.Clear();
            this.ddlCategoryId.Items.Add(new ListItem("请选择类别", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["title"].ToString().Trim();

                this.ddlCategoryId.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.xinxi bll = new BLL.xinxi();
            Model.xinxi model = bll.GetModel(_id);

            if (model.type_id == 3)
            {
                div_category.Visible = false;
                div_price.Visible = false;
                div_size.Visible = false;
            }
            else
            {
                div_category.Visible = true;
                div_price.Visible = true;
                div_size.Visible = true;
            }

            rblTypeID.SelectedValue = model.type_id.ToString();
            ddlCategoryId.SelectedValue = model.category_id.ToString();
            txtPrice.Text = model.price.ToString();
            ddlUnit.SelectedValue = model.unit.ToString();
            txtLong.Text = model.size_long.ToString();
            txtWide.Text = model.size_wide.ToString();
            txtHeight.Text = model.size_height.ToString();
            txtContent.Text = Utils.ToTxt(model.content);
            if (model.quanzi_id > 0)
            {
                txtQuanziID.Text = model.quanzi_id.ToString();
                lbQuanziName.Text = new BLL.quanzi().GetTitle(model.quanzi_id);
            }
            txtMobile.Text = model.mobile;
            txtMobile.ReadOnly = true;
            txtMobile.Attributes.Remove("ajaxurl");
            txtAddTime.Text = model.add_time.ToString("yyyy-MM-dd HH:mm:ss");
            rptAlbumList.DataSource = model.albums;
            rptAlbumList.DataBind();
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            try
            {
                Model.xinxi model = new Model.xinxi();
                BLL.xinxi bll = new BLL.xinxi();

                if (rblTypeID.SelectedValue != "3")
                {
                    model.category_id = Utils.StrToInt(ddlCategoryId.SelectedValue, 0);
                    model.price = Utils.StrToInt(txtPrice.Text.Trim(), 0);
                    model.unit = ddlUnit.SelectedValue.ToString();
                    model.size_long = Utils.StrToInt(txtLong.Text.Trim(), 0);
                    model.size_wide = Utils.StrToInt(txtWide.Text.Trim(), 0);
                    model.size_height = Utils.StrToInt(txtHeight.Text.Trim(), 0);
                    model.content = txtContent.Text;
                }
                model.content = Utils.ToHtml(txtContent.Text);
                model.mobile = txtMobile.Text.Trim();
                model.add_time = Utils.StrToDateTime(txtAddTime.Text.Trim());
                model.type_id = int.Parse(rblTypeID.SelectedValue);
                model.quanzi_id = Utils.StrToInt(txtQuanziID.Text.Trim(), 0);
                model.status = 0;
                model.hits = 0;

                Model.users userModel = new BLL.users().GetMobileModel(model.mobile);
                if (userModel != null)
                {
                    model.user_id = userModel.id;
                    model.company_id = userModel.company_id;
                    model.lianxiren = userModel.nick_name;
                    model.province_id = userModel.province_id;
                    model.city_id = userModel.city_id;
                }

                #region 保存相册====================
                //检查是否有自定义图片
                string[] albumArr = Request.Form.GetValues("hid_photo_name");
                string[] remarkArr = Request.Form.GetValues("hid_photo_remark");
                if (albumArr != null && albumArr.Length > 0)
                {
                    List<Model.xinxi_albums> ls = new List<Model.xinxi_albums>();
                    for (int i = 0; i < albumArr.Length; i++)
                    {
                        string[] imgArr = albumArr[i].Split('|');
                        if (imgArr.Length == 3)
                        {
                            if (!string.IsNullOrEmpty(remarkArr[i]))
                            {
                                ls.Add(new Model.xinxi_albums { original_path = imgArr[1], thumb_path = imgArr[2], remark = remarkArr[i] });
                            }
                            else
                            {
                                ls.Add(new Model.xinxi_albums { original_path = imgArr[1], thumb_path = imgArr[2] });
                            }
                        }
                    }
                    model.albums = ls;
                }
                #endregion

                if (bll.Add(model) > 0)
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            try
            {
                BLL.xinxi bll = new BLL.xinxi();
                Model.xinxi model = bll.GetModel(_id);

                if (rblTypeID.SelectedValue != "3")
                {
                    model.category_id = Utils.StrToInt(ddlCategoryId.SelectedValue, 0);
                    model.price = Utils.StrToInt(txtPrice.Text.Trim(), 0);
                    model.unit = ddlUnit.SelectedValue.ToString();
                    model.size_long = Utils.StrToInt(txtLong.Text.Trim(), 0);
                    model.size_wide = Utils.StrToInt(txtWide.Text.Trim(), 0);
                    model.size_height = Utils.StrToInt(txtHeight.Text.Trim(), 0);
                    model.content = txtContent.Text;
                }
                model.content = Utils.ToHtml(txtContent.Text);
                model.mobile = txtMobile.Text.Trim();
                model.add_time = Utils.StrToDateTime(txtAddTime.Text.Trim());
                model.type_id = int.Parse(rblTypeID.SelectedValue);
                model.quanzi_id = Utils.StrToInt(txtQuanziID.Text.Trim(), 0);

                #region 保存相册====================
                if (model.albums != null)
                {
                    model.albums.Clear();
                }
                string[] albumArr = Request.Form.GetValues("hid_photo_name");
                string[] remarkArr = Request.Form.GetValues("hid_photo_remark");
                if (albumArr != null)
                {
                    List<Model.xinxi_albums> ls = new List<Model.xinxi_albums>();
                    for (int i = 0; i < albumArr.Length; i++)
                    {
                        string[] imgArr = albumArr[i].Split('|');
                        int img_id = Utils.StrToInt(imgArr[0], 0);
                        if (imgArr.Length == 3)
                        {
                            if (!string.IsNullOrEmpty(remarkArr[i]))
                            {
                                ls.Add(new Model.xinxi_albums { id = img_id, article_id = _id, original_path = imgArr[1], thumb_path = imgArr[2], remark = remarkArr[i] });
                            }
                            else
                            {
                                ls.Add(new Model.xinxi_albums { id = img_id, article_id = _id, original_path = imgArr[1], thumb_path = imgArr[2] });
                            }
                        }
                    }
                    model.albums = ls;
                }
                #endregion

                if (bll.Update(model))
                {
                    return true;
                }
            }
            catch(Exception ex)
            {
                Utils.WriteTextLog("xinxi_save",ex.Message,DateTime.Now);
                return false;
            }
            return false;
        }
        #endregion

        //保存类别
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {

                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改内容成功！", "xinxi_list.aspx");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加内容成功！", "xinxi_list.aspx");
            }
        }
        //选择发送类型
        protected void rblTypeID_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rblTypeID.SelectedValue == "3")
            {
                div_category.Visible = false;
                div_price.Visible = false;
                div_size.Visible = false;
            }
            else
            {
                div_category.Visible = true;
                div_price.Visible = true;
                div_size.Visible = true;
            }
        }
    }
}